package com.ycfmedical.bed.utils;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bBcApplication;
import com.ycfmedical.bed.ui.MusicPlayActivity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

/**
 * Created by Egoist on 2017/1/19.
 */

public class ResourceUtil {


    public static Resources getResources() {
        return bBcApplication.getContext().getResources();
    }

    public static Context getContext() {
        return bBcApplication.getContext();
    }
    
    
    public static void showFucNoOpenToast(Context context)
    {
        Toast.makeText(context, "该功能暂未开放", Toast.LENGTH_SHORT).show();
    }


    public static Drawable getForegroundDrawable(int musicPicRes,Context context)
    {
        Context mContext = context;
        /* 得到屏幕的宽高比，以便按比例切割图片一部分 */
        final float widthHeightSize =
                (float)(DisplayUtil.getScreenWidth(mContext) * 1.0 / DisplayUtil.getScreenHeight(mContext) * 1.0);

        Bitmap bitmap = getForegroundBitmap(musicPicRes,mContext);
        int cropBitmapWidth = (int)(widthHeightSize * bitmap.getHeight());
        int cropBitmapWidthX = (int)((cropBitmapWidth - bitmap.getWidth()) / 2.0);

        /* 切割部分图片 */
        //Bitmap cropBitmap = Bitmap.createBitmap(bitmap, cropBitmapWidth, 0, cropBitmapWidthX, bitmap.getHeight());
        /* 缩小图片 */
        Bitmap scaleBitmap =
                Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 50, bitmap.getHeight() / 50, false);
        /* 模糊化 */
        final Bitmap blurBitmap = FastBlurUtil.doBlur(scaleBitmap, 8, true);

        final Drawable foregroundDrawable = new BitmapDrawable(blurBitmap);
        /* 加入灰色遮罩层，避免图片过亮影响其他控件 */
        foregroundDrawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        return foregroundDrawable;
    }

    public static Bitmap getForegroundBitmap(int musicPicRes,Context context)
    {
        Context mContext = context;
        int screenWidth = DisplayUtil.getScreenWidth(mContext);
        int screenHeight = DisplayUtil.getScreenHeight(mContext);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeResource(getResources(), musicPicRes, options);
        int imageWidth = options.outWidth;
        int imageHeight = options.outHeight;

        if (imageWidth < screenWidth && imageHeight < screenHeight)
        {
            return BitmapFactory.decodeResource(getResources(), musicPicRes);
        }

        int sample = 2;
        int sampleX = imageWidth / DisplayUtil.getScreenWidth(mContext);
        int sampleY = imageHeight / DisplayUtil.getScreenHeight(mContext);

        if (sampleX > sampleY && sampleY > 1)
        {
            sample = sampleX;
        }
        else if (sampleY > sampleX && sampleX > 1)
        {
            sample = sampleY;
        }

        options.inJustDecodeBounds = false;
        options.inSampleSize = sample;
        options.inPreferredConfig = Bitmap.Config.RGB_565;

        return BitmapFactory.decodeResource(getResources(), musicPicRes, options);
    }
}
